package vip.websky.project.web.controller.user;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.EditPermissionReqDTO;
import vip.websky.project.module.dto.request.user.GetPermissionReqDTO;
import vip.websky.project.user.service.PermissionService;
import vip.websky.project.web.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/27-19:59
 **/
@Slf4j
@Controller
@RequestMapping("/menu/*")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value = {"toList"}, method = RequestMethod.GET)
    public String toList() {
        return "/user/menuList";
    }

    @RequestMapping(value = {"toEdit"}, method = RequestMethod.GET)
    public String toEdit() {
        return "/user/menuEdit";
    }

    @RequestMapping(method = RequestMethod.POST)
    public void editMenu(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                         EditPermissionReqDTO menuReqDTO) {
        String param = JSON.toJSONString(menuReqDTO);
        log.info("调用[PermissionController-editMenu]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data;
        if (menuReqDTO.getId() == null || menuReqDTO.getId().equals("")) {
            data = permissionService.addPermission(requestDTO);
        } else {
            data = permissionService.editPermission(requestDTO);
        }
        sendMessages(request, response, data);
    }

    @RequestMapping(method = RequestMethod.GET)
    public void getMenuList(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                            GetPermissionReqDTO menuReqDTO) {
        String param = JSON.toJSONString(menuReqDTO);
        log.info("调用[PermissionController.getMenuList]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = permissionService.getPermissionList(requestDTO);
        sendMessages(request, response, data.getResult());
    }

    @RequestMapping(value = {"{id}"}, method = RequestMethod.GET)
    public ModelAndView getMenu(RequestDTO requestDTO, ModelAndView view,
                                GetPermissionReqDTO menuReqDTO) {
        String param = JSON.toJSONString(menuReqDTO);
        log.info("调用[PermissionController.getMenu]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = permissionService.getPermission(requestDTO);
        view.addObject("menu", data.getResult());
        view.setViewName("/user/menuEdit");
        return view;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public void removeMenu(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDTO,
                           GetPermissionReqDTO menuReqDTO) {
        String param = JSON.toJSONString(menuReqDTO);
        log.info("调用[PermissionController.removeMenu]方法:参数: {}", param);
        requestDTO.setParam(param);
        ResponseDTO data = permissionService.removePermission(requestDTO);
        sendMessages(request, response, data);

    }

}
